/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.sigawe.SIASDIT.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sigawe.SIASDIT.dao.interfc.IInbox;
import net.sigawe.SIASDIT.dao.model.Message;


/**
 *
 * @author yoze
 */
public class InboxDAO implements IInbox {
    
    private Connection connection;
    PreparedStatement ps;
    ResultSet rs;

    public InboxDAO(Connection connection) {
        this.connection = connection;
}

    public List<Message> getAll() throws SQLException {
        Message inbox = new Message();
        List<Message> list = new ArrayList<Message>();
        String sql = "SELECT id,Sender,Text FROM inbox";
        //PreparedStatement
        ps = connection.prepareStatement(sql);
        //ResultSet
        rs = ps.executeQuery();
        rs.beforeFirst();
        while (rs.next()) {
            Message message = new Message();
            message.setIdMessage(rs.getInt("id"));
            message.setPhoneNumber(rs.getString("Sender"));
            message.setText(rs.getString("Text"));
            list.add(message);
            
            }
        closeAll();
        return list;
        }

public void closeAll()
    {
        try {
            ps.close();
//            psIn.close();
            rs.close();
        //    konek.close();
        } catch (SQLException ex) {
            Logger.getLogger(InboxDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void delete(int id) throws SQLException {
        String sql = "DELETE FROM inbox WHERE id=?";
        //PreparedStatement
        ps = connection.prepareStatement(sql);
        ps.setInt(1, id);
        ps.executeUpdate();
    }

   
}
